using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace XnaUtility.Camera
{
    public class MouseLookCamera : CameraBase
    {

        private Control.InputBase input;
        Vector3 direction;
        Vector3 rotation;

        public MouseLookCamera(Game game, float fov, float ar, float near, float far)
            : base(game, fov, ar, near, far)
        {
            direction = new Vector3(0, 0, -1);
            Location = new Vector3(0, 0, 0);
            rotation = new Vector3(0, 0, 0);
        }

        public override void Initialize()
        {
            input = (XnaUtility.Control.InputBase)Game.Services.GetService(typeof(Control.InputBase));

            if (input == null) throw new Exception("KeyboardCamera failed to get input service");

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {

            if (input.IsMouseButtonDown(XnaUtility.Control.MouseButton.LeftButton))
            {
                rotation.Y = input.MouseDelta.X * 0.001f;
                direction = Vector3.Transform(direction, Matrix.CreateFromAxisAngle(Vector3.UnitY, rotation.Y));

                Vector3 move = direction * input.MouseDelta.Y * 0.25f *-1;
                Location += move;
            }
            else if (input.IsMouseButtonDown(XnaUtility.Control.MouseButton.RightButton))
            {
                rotation.Y = input.MouseDelta.X * 0.001f;
                rotation.X = input.MouseDelta.Y * 0.001f;
                direction = Vector3.Transform(direction, Matrix.CreateFromAxisAngle(Vector3.UnitY, rotation.Y));
                direction = Vector3.Transform(direction, Matrix.CreateFromAxisAngle(Vector3.UnitX, rotation.X));
            }

            target = Location + direction;

            base.Update(gameTime);
        }

    }
}
